<template>
  <c-dialog ref="dialog" :data="selData" single title="安排工程监理" @closed="closedHandle">
    <c-list ref="form" :model="formData" :rules="formRules" label-width="80px">
      <c-list-item prop="proSupervisorCode" label="工程监理">
        <c-single-dialog
          ref="proSupervisorCode"
          v-model="formData.proSupervisorCode"
          :label.sync="formData.proSupervisorName"
          :props="{label:'userName',value:'account'}"
          :focus-query="true"
          :multiple="false"
          :config="{
            title:'工程监理',
            width:'600px',
            interfacePath:`/slm-admin/api/v1/user/listUsers`,
            method:'post',
            filtersRaw:[],
            paramsRaw:{},
            setting:[
              {prop:'account',title:'编码',width:80},
              {prop:'userName',title:'名称',width:200}
            ]
          }"
          @change="changeData"
        />
      </c-list-item>
      <c-list-item label="部门">
        {{ orgInfo }}
      </c-list-item>
      <c-list-item>
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button type="primary" @click="confirmHandle">确认</c-button>
        </div>
      </c-list-item>
    </c-list>
  </c-dialog>
</template>
<script>
import { postSetSupervisor } from '@/api/decorate'
import { getOrgByAccount } from '@/api/common'

export default {
  name: 'AllocationSupervision',
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      orgInfo: '',
      supervision: [],
      formData: {
        proSupervisorCode: '',
        proSupervisorName: ''
      },
      formRules: {
        proSupervisorCode: [{ required: true, message: '请选择工程监理', trigger: 'change' }]
      }
    }
  },
  computed: {
    detail() {
      return this.selData.length === 1 ? this.selData[0] : {}
    }
  },
  methods: {
    changeData(data) {
      if (!data) return
      getOrgByAccount(data.account).then(res => {
        if (res.success === 1) {
          if (res.data.length !== 0) {
            const list = res.data.map(el => el.orgName)
            this.orgInfo = list.join(',')
          } else {
            this.orgInfo = ''
          }
        }
      })
    },
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        const data = {
          id: this.detail.id,
          proSupervisorCode: this.formData.proSupervisorCode,
          proSupervisorName: this.formData.proSupervisorName
        }
        return postSetSupervisor(data, 'add').then(res => {
          this.$message.success('操作成功！')
          this.$emit('on-success')
          this.$refs.dialog.visible = false
        })
      })
    },
    cancelHandle() {
      this.$refs.dialog.visible = false
    },
    closedHandle() {
      this.supervision = []
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
